package com.asset.dispoal.commom.utils;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5 {

    public static String getMd5Code(String source, int length) throws NoSuchAlgorithmException {
        String result = "";
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(source.getBytes());
        byte[] b = md.digest();
        int i;
        StringBuilder buf = new StringBuilder();
        for (byte value : b) {
            i = value;
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(i));
        }
        result = buf.toString();
        if (length != 32) {
            result = buf.substring(8, 24);
        }

        return result;
    }
}
